package com.example.signupservice.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.commonservice.Common.Result;
import com.example.signupservice.entity.ActivityCheckin;
import com.example.signupservice.entity.ActivitySignup;
import com.example.signupservice.feign.UserFeignClient;
import com.example.signupservice.service.ActivityCheckinService;
import com.example.signupservice.service.ActivitySignupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("signup")
public class ActivitySignupController {

    @Autowired
    private ActivitySignupService signupService;
    @Autowired
    private UserFeignClient userFeignClient;
    @Autowired
    private ActivityCheckinService checkinService;

    /**
     * 志愿者报名活动
     */
    @PostMapping("/sign-up")
    public Result<String> signUp(@RequestParam Long activityId, @RequestParam Long volunteerId) {
        boolean success = signupService.signUp(activityId, volunteerId);
        return success ? Result.success("报名成功") : Result.error("已报名过该活动，请勿重复提交");
    }

    /**
     * 删除报名记录（管理员）
     */
    @PostMapping("/delete")
    public Result<String> deleteSignup(@RequestParam Long id) {

        //删除签到的记录
        ActivitySignup activitySignup = signupService.getById(id);

        Long volunteerId = activitySignup.getVolunteerId();
        boolean success = signupService.removeById(id);
        boolean success1 = checkinService.delete(id,volunteerId);
        return success ? Result.success("删除成功") : Result.error("记录不存在，删除失败");
    }

    /**
     * 更新报名状态（如签到、签退）
     */
    @PostMapping("/update-status")
    public Result<String> updateStatus(@RequestBody ActivitySignup signup) {
        boolean success = signupService.updateById(signup);
        return success ? Result.success("状态更新成功") : Result.error("记录不存在，更新失败");
    }

    /**
     * 条件分页查询报名记录
     */
    @GetMapping("/list")
    public Result<IPage<ActivitySignup>> listSignups(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) Long activityId,
            @RequestParam(required = false) Long volunteerId,
            @RequestParam(required = false) Integer status) {

        Map<String, Object> params = new HashMap<>();
        params.put("activityId", activityId);
        params.put("volunteerId", volunteerId);
        params.put("status", status);

        return Result.success(signupService.pageSignups(current, size, params));
    }
    // 移除原有的评价接口
    // @PostMapping("/evaluate")
    // public Result<String> evaluate(...)

    /**
     * 新增报名接口（匹配前端数据结构）
     */
    @PostMapping("/add")
    public Result<String> addSignup(@RequestBody ActivitySignup signup) {
        signup.setSignupTime(LocalDateTime.now());

        try {
              //获取volunteerId
            Result<Long> volunteerId = userFeignClient.getVolunteerId(signup.getUserId());
            System.out.println("volunteerId"+volunteerId);
            if (volunteerId == null) {
                return Result.error("无法获取志愿者ID");
            }
            signup.setVolunteerId(volunteerId.getData());
            //signup.setVolunteerId(1L);
            Long volunteerId1 = signup.getVolunteerId();
            boolean success = signupService.save(signup);
            if (success) {
                // 获取并打印新增的ID
                Long generatedId = signup.getId();
                System.out.println("新增记录的ID: " + generatedId);
                //添加签到签退记录
                ActivityCheckin activityCheckin = new ActivityCheckin();
                activityCheckin.setSignupId(generatedId);
                activityCheckin.setVolunteerId(volunteerId1);

                boolean success1 = checkinService.addCheckinRecord(activityCheckin);

                return Result.success("报名成功");
            } else {
                return Result.error("报名失败");
            }
        } catch (Exception e) {
            return Result.error("调用用户服务失败: " + e.getMessage());
        }
    }
}
